// Engine includes
#include "Vector3.h"
#include "Camera.h"
#include "pg2_directinput.h"

#include "CamMover.h"

CamMover::CamMover(MegaEngine::Camera* camera, pg2::DirectInput* input)
{
	this->camera = camera;
	this->input = input;

	camPos = new MegaEngine::Vector3(0,0,10);
}

CamMover::~CamMover()
{
	delete camPos;
}

void CamMover::OnLoop()
{
	
	if(input->keyDown(pg2::Input::KEY_W))
		{//forward
			*camPos += (camera->GetForward() * 0.001f);
		}
		if(input->keyDown(pg2::Input::KEY_S))
		{//backward
			*camPos -= (camera->GetForward() * 0.001f);
		}
		if(input->keyDown(pg2::Input::KEY_A))
		{//left
			//camX += 0.01f;
			*camPos += (camera->GetForward().Cross(camera->GetUpward()) * 0.001f);
		}
		if(input->keyDown(pg2::Input::KEY_D))
		{//right
			*camPos -= (camera->GetForward().Cross(camera->GetUpward()) * 0.001f);
		}
		if(input->keyDown(pg2::Input::KEY_SPACE))
		{//up
			//camY -= 0.01f;
			*camPos += (camera->GetUpward() * 0.001f);
		}
		if(input->keyDown(pg2::Input::KEY_X))
		{//down
			*camPos -= (camera->GetUpward() * 0.001f);
		}
		
		if((input->mouseRelPosX() > 1) || input->mouseRelPosX() < -1)
		{
			MegaEngine::Vector3 newForward = camera->GetForward();

			newForward = newForward.RotateAround(camera->GetUpward(), input->mouseRelPosX()*0.01f);

			camera->SetForward(newForward);
		}/**/

		if((input->mouseRelPosY() > 1) || input->mouseRelPosY() < -1)
		{
			MegaEngine::Vector3 newForward = camera->GetForward();

			newForward = newForward.RotateAround(camera->GetLeft(), input->mouseRelPosY()*(0.01f));

			camera->SetForward(newForward);
		}

		if(*camPos != camera->GetPosition())
			camera->SetPosition(*camPos);
}